import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/viewmodel/service_order/add_service_order_view_model.dart';
import 'package:ls_work/widgets/add_image_widget.dart';
import 'package:ls_work/widgets/bottom_input_room_address_widget.dart';
import 'package:ls_work/widgets/bottom_input_widget.dart';
import 'package:ls_work/widgets/input_text_page.dart';
import 'package:ls_work/widgets/title_value_widget.dart';

class AddServiceOrderPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _AddServiceOrderState();
}

class _AddServiceOrderState
    extends BaseMvvmState<AddServiceOrderViewModel, AddServiceOrderPage> {
  @override
  Widget buildLoadingContentView() {
    return ListView(children: [
      TitleValueWidget(
        '类型',
        isRequired: true,
        text: viewModel.currentType?.name ?? "",
      ).onTap(() {
        viewModel.selectRepairType();
      }),
      Gaps.dividerLine,
      viewModel.currentType?.codeValue=="1"
          ? TitleValueWidget(
              '服务对象',
              isRequired: true,
              hint: '请选择',
              text: viewModel.old?.name ?? "",
            ).onTap(() {
              viewModel.selectOld();
            })
          : Row(
              children: [
                CommonText('服务对象').setWeight(2),
                CommonText(
                  viewModel.inputHuman,
                  hintText: '请输入服务对象',
                  textSize: 15
                ).onTap(() {
                  showBottomInput(context,title: "服务对象",hint: "请输入服务对象",content: viewModel.inputHuman,onSubmit: (str){
                    setState(() {
                      viewModel.inputHuman = str;
                    });
                  });
                })
                    .addToContainer(
                        decoration: BoxDecoration(color: Colours.bg_color),
                        padding:
                            EdgeInsets.symmetric(horizontal: 8, vertical: 8))
                    .setWeight(3)
              ],
            ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10)),
      Gaps.dividerLine,
      viewModel.currentType?.codeValue=="1"
          ? TitleValueWidget(
              '服务地点',
              hint: '选择服务对象后自动获取',
              showArrow: false,
              isRequired: true,
              text: viewModel.oldAddress,
            )
          : Row(
              children: [
                Text.rich(TextSpan(
                  text: '*',style: TextStyle(color: Colors.red,fontSize: 16),
                  children: [
                    TextSpan(
                      text: '服务地点',
                      style: TextStyle(color: Colors.black,fontSize: 16),
                    )
                  ]
                )).setWeight(2),
                CommonText(
                  viewModel.inputAddress,
                  hintText: '请输入服务地点',
                  textSize: 15
                ).onTap(() {
                  showBottomInputRoomAddress(context,title: "服务地点",hint: "请输入服务地点",content: viewModel.inputAddress,onSubmit: (str){
                    setState(() {
                      viewModel.inputAddress = str;
                    });
                  });
                })
                    .addToContainer(
                        decoration: BoxDecoration(color: Colours.bg_color),
                        padding:
                            EdgeInsets.symmetric(horizontal: 8, vertical: 8))
                    .setWeight(3)
              ],
            ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10)),
      Gaps.dividerLine,
      TitleValueWidget(
        '楼栋',
        isRequired: false,
        text: viewModel.buildingName,
      ).onTap(() {
        viewModel.selectBuildingAndFloor();
      }),
      Gaps.dividerLine,
      TitleValueWidget(
        '楼层',
        isRequired: false,
        text: viewModel.floorName,
      ).onTap(() {
        viewModel.selectBuildingAndFloor();
      }),
      Gaps.dividerLine,
      TitleValueWidget(
        '预约时间',
        isRequired: false,
        text: viewModel.serviceTime,
      ).onTap(() {
        viewModel.selectServiceTime();
      }),
      Gaps.dividerLine,
      TitleValueWidget(
        '服务项目',
        isRequired: true,
        text: viewModel.serviceItem?.name ?? "",
      ).onTap(() {
        viewModel.selectServiceItem();
      }),
      Gaps.dividerLine,
      TitleValueWidget(
        '服务内容',
        isRequired: true,
        text: viewModel.serviceContent?.name ?? "",
      ).onTap(() {
        viewModel.selectServiceContent();
      }),
      Gaps.dividerLine,
      // TitleValueWidget(
      //   '服务部门',
      //   isRequired: true,
      //   text: viewModel.department?.officeName ?? "",
      // ).onTap(() {
      //   viewModel.selectDepartment();
      // }),
      // Gaps.dividerLine,
      CommonText('备注').addToContainer(
          padding: EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 10),
          width: double.infinity),
      CommonInput(
        border: InputBorder.none,
        isDense: true,
        maxLines: 4,
        hintText: '请输入备注信息',
        padding: EdgeInsets.zero,
        onChanged: (str) {
          viewModel.remark = str;
        },
      ).addToContainer(
          margin: EdgeInsets.only(left: 16, right: 16, bottom: 16),
          padding: EdgeInsets.all(8),
          decoration: BoxDecoration(
              color: Colours.bg_color, borderRadius: 8.borderRadius)),
      Gaps.dividerLine,
      CommonText('现场照片').addToContainer(
          padding: EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 8),
          width: double.infinity),
      AddImageWidget(
        (files) {
          viewModel.files = files;
        },
        files: viewModel.files,
        max: 3,
        cameramsg: "拍照工单现场照片，需要申请您的相机权限",
        photomsg: "从相册选择工单现场照片，需要申请您的相册权限",
      ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 8)),
      Gaps.dividerLine,
      ElevatedButton(
        onPressed: () {
          viewModel.submitClick();
        },
        child: CommonText('提交', textColor: Colors.white),
        style: ButtonStyle(
            shape: WidgetStateProperty.all(StadiumBorder()),
            elevation: WidgetStateProperty.all(0),
            minimumSize: WidgetStateProperty.all(Size(double.infinity, 40))),
      ).addToContainer(
          margin: EdgeInsets.symmetric(horizontal: 16, vertical: 14))
    ]);
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('新增服务单'),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  AddServiceOrderViewModel createViewModel() {
    return AddServiceOrderViewModel(context);
  }

  @override
  void onRetryClick() {}
}
